// var urls = window.location.origin
// var urls = 'http://tw.papergenerate.tw.local'
var sex = [
	"交互式",
	"山东省",
	"湖南省",
	"地下停",
	"B2B",
	"浅谈上",
	"小学卫",
	"浅析中",
	"三角形",
	"时间额",
	"新技术",
	"倾向于",
	"校内外",
	"虚拟网",
	"虚拟机",
	"前端开",
	"单片机",
	"信息化",
	"机电一",
	"磨床的",
	"公路建",
	"桥梁基",
	"广东省",
	"沥青与",
	"显示器",
	"谈胰腺",
	"H农业",
	"制造业",
	"工业与",
	"污水治",
	"市场影",
	"发电机",
	"风力发",
	"顺丰冷",
	"混泥土",
	"刷式密",
	"抗生素",
	"天津城",
	"共享单",
	"A公司",
	"生态环",
	"高管团",
	"交通银",
	"给排水",
	"北京市",
	"基于随",
	"一罪与",
	"醉酒驾",
	"与非编",
	"简述韩",
	"数字化",
	"土壤肝",
	"针灸､推",
	"虚拟电",
	"契约受",
	"家用热",
	"充分利",
	"地铁盾",
	"宜昌市",
	"越橘杂",
	"红参提",
	"长岭县",
	"美食实",
	"区角活"
]
var SPECIALITY = [
    '汉语国际教育',
    '工程造价',
    '建筑工程技术',
    '环境艺术设计',
    '艺术设计',
    '护理',
    '公共事业管理',
    '工商企业管理',
    '汽车检测与维修技术',
    '汽车技术服务与营销',
    '计算机网络技术',
    '影视多媒体技术',
    '软件技术',
    '高速铁路客运服务',
    '现代物流管理',
    '物联网应用技术',
    '无人机应用技术',
    '大数据与会计',
    '公共文化服务与管理',
    '烹饪工艺与营养',
    '市场营销',
    '学前教育',
    '会计信息管理',
    '大数据与财务管理',
    '体育运营与管理',
    '酒店管理',
    '计算机应用技术',
    '移动互联应用技术',
    '城市轨道交通运营管理',
    '电子商务',
    '汽车制造与试验技术',
    '新能源汽车技术',
    '旅游管理',
    '经济',
    '教育',
    '文学',
    '医药',
    '法律',
    '计算机科学',
    '建筑科学',
    '工业技术',
	'其他（自动识别）'
];
// 封装url参数获取
function getQueryVariable(variable){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if(pair[0] == variable){return decodeURI(pair[1]);}
	}
	return(false);
}

let act_code = ''
if(getQueryVariable('ac')){
	act_code = getQueryVariable('ac')
	window.localStorage.setItem('act_code',getQueryVariable('ac'))
}else{
	if(window.localStorage.getItem('act_code')){
		act_code = window.localStorage.getItem('act_code')
	}
}
let suffix = ''
if(act_code){
	suffix = 'act_code='+ act_code +'&_session_type=user'
}

var urls = 'https://api.taoxiangyoushu.com'//http://api.project_libraries.report
function getFormData(object) {
    // 转FromData 对象
    var formData = new FormData();
    Object.keys(object).forEach(function (key) {
        var value = object[key];
        if (Array.isArray(value)) {
            value.forEach(function (subValue, i) {
                formData.append(key + "[" + i + "]", subValue);
            });
        } else {
            formData.append(key, object[key]);
        }
    });
    return formData;
}

var selling_price = ''
var isclick = false
var parameterSet = {}
var threeMsg = {}
var payWayInfo= {}

window.onload = function (){
	$.ajax({
		type: 'post',
		url: urls + "/api/project/info?user_token="+USER_TOKEN+"&jane_name="+JANE_NAME +(suffix?'&'+suffix:''),
		xhrFields: {
			withCredentials: true
		},
		success: function success(result) {
			if(result.code == '200') {
				var data = result.data
				// var info = data.project[0].goods_info[0]
				// selling_price = info.selling_price

				var seller_info = data.seller_info
				threeMsg = data.project[0].threeMsg
				$(".customerBox .customer-title").text(seller_info.kf_title || '联系客服')
				$(".customerBox .customer-qr").attr('src', seller_info.kf_qr || '')
				$('.customerBox .qqText .s1').text(seller_info.qq || '')
				$('.customerBox .telephone .s1').text(seller_info.phone || '')
				$('.customerBox .mailbox .s1').text(seller_info.email || '')
				$('.customerBox .wxText .s1').text(seller_info.wx || '')
				if(seller_info.qq) $('.customerBox  .qqText').show();
				if(seller_info.phone) $('.customerBox  .telephone').show();
				if(seller_info.email) $('.customerBox  .mailbox').show();
				if(seller_info.wx) $('.customerBox  .wxText').show();
				if(seller_info.kf_qr){
					$(".customerBox .customerCode").show()
				}else{
					$(".customerBox .customerCode").hide()
				}
				if(seller_info.qq || seller_info.phone || seller_info.email || seller_info.wx){
					$(".customerBox .customerWay").show()
				}else{
					$(".customerBox .customerWay").hide()
				}
				if(seller_info.kf_qr && (seller_info.qq || seller_info.phone || seller_info.email || seller_info.wx)){
					$(".customerBox .segmentation-line").show()
				}else{
					$(".customerBox .segmentation-line").hide()
				}
				if(!seller_info.qq && !seller_info.phone && !seller_info.email && !seller_info.wx && !seller_info.kf_qr){
					$(".noCustomer").show()
				}else{
					$(".noCustomer").hide()
				}
				//绑定微信需要 确保代理是否绑定微信
				if($('.queryApp').length){
					startQuerying()
				}
				// $('#price').text('￥' + selling_price + '/千字')
				// 重组数据
				typefun(data.project[0].goods_info)
				setArguments(data.project[0].goods_info)
				isclick = true
				// if($('.pointOut').length) {
				// 	showOrder(data.project[0].pay_way)
				// }

				if($('#payHtml').length) {
					payWay(data.project[0].pay_way , data.project[0].goods_info)
				}
				if($('.indexApp').length) {
					dropDownType(result.data.project[0] && result.data.project[0].goods_info)
				}
				payWayInfo = data.project[0].pay_way
			}else{
				cocoMessage.error(result.codeMsg, 2000);
			}


			// pay的二维码需要后端支付类型,
			// 本意是在index存储后, pay判断是否存在数据,
			// 存在数据就可以减少二维码的刷新时间, 不必等待info的接口完成后刷新二维码
			// 但sessionStorage在iframe会发生找不到的情况报错

			// if(!(sessionStorage.getItem("pay_way")) && $('#payHtml').length) {
			// payWay(data.project[0].pay_way)
			// }
			// sessionStorage.setItem("pay_way", JSON.stringify(data.project[0].pay_way));
		}
	});
}

var typeData = {}
var WordCount_data = {
	bylw: [5000 , 10000 , 30000 , 50000],
	qklw: [3000 , 5000 , 8000 , 10000],
	bylwsenior: [5000 , 10000 , 30000 , 50000],
	sxbg: [1000 , 3000 , 5000 , 10000]
}
var Radius_data = {
	bylw: [5000 , 50000],
	qklw: [3000 , 10000],
	bylwsenior: [5000 , 50000],
	sxbg: [1000 , 30000]
}
function typefun(goods_info) {
	for(var i=0; i<goods_info.length; i++) {
		typeData[goods_info[i].goods_id] = {
			WordCount: WordCount_data[goods_info[i].short_name] || [],
			Radius: Radius_data[goods_info[i].short_name] || [],
			short_name: goods_info[i].short_name,
			increment_goods_list: goods_info[i].increment_goods_list,
			increment_goods_infos: goods_info[i].increment_goods_infos,
			selling_price: goods_info[i].selling_price,
			unit_type: goods_info[i].unit_type,
			unit_count: goods_info[i].unit_count,
			name: goods_info[i].name,
			type: goods_info[i].type
		}
	}
}
function setArguments(info) {
	for (var i=0;i<info.length; i++){
		parameterSet[info[i].short_name] = info[i].goods_config
	}
}

$(function (){
	window.setHeight = function (){
		$(".boxLeft").css('height', 'auto')
		$(".rightCont").css('height', 'auto')
		if($('.boxLeft').outerHeight() > $(".rightCont").outerHeight()){
			$(".rightCont").css('height', $('.boxLeft').outerHeight())
			$(".foot").show()
		}

		if($(window).width() > 994){
			if(window.sessionStorage.getItem('editionKey') == 'advanced' && $('.rightCont').height() < 982){
				$(".boxLeft").css('height', '982px')
				return
			}
			$(".boxLeft").css('height',$('.rightCont').height())
		}else{
			$(".boxLeft").css('height','auto')
		}

	}
	setHeight()
	window.onresize = function (){
		window.setHeight()
	}
})

$('.Toggle2').click(function () {
	if(!isclick) return;
	$('.VersionSwitchingLoding').show()
	var this_ = $(this)
	setTimeout(function() {
		$('.VersionSwitchingLoding').hide();
		editionType(this_.attr('edition-key') , true)
		if(this_.attr('edition-key') == 'Normal'){		// 切换后
			if(parameterSet.bylwsenior.submit_attribute.value.education.value){
				$(".education_c").show()
			}else{
				$(".education_c").hide()
			}
			if(parameterSet.bylwsenior.submit_attribute.value.keyword.value){
				$(".keywordBox").show()
			}else{
				$(".keywordBox").hide()
			}
			if(parameterSet.bylwsenior.submit_attribute.value.major.value){
				$(".major").show()
			}else{
				$(".major").hide()
			}
			$(".EnglishLiterature").show()
		}
		if(this_.attr('edition-key') == 'advanced'){	// 切换后
			if(parameterSet.bylw.submit_attribute.value.education.value && (typeData[$("#type_s").val()].short_name == 'bylw')){
				$(".education_c").show()
			}else{
				$(".education_c").hide()
			}
			if(parameterSet.bylw.submit_attribute.value.keyword.value && (typeData[$("#type_s").val()].short_name == 'bylw')){
				$(".keywordBox").show()
			}else{
				$(".keywordBox").hide()
			}
			if(parameterSet.bylw.submit_attribute.value.major.value && (typeData[$("#type_s").val()].short_name == 'bylw')){
				$(".major").show()
			}else{
				$(".major").hide()
			}
			$(".proposal-check").trigger('change')
		}
	}, 500)
});

function editionType(edition , is) {
	$('#App').removeClass('bylwsenior wxzs ktbg rws qklw kclw dybg zjcaigc sxbg lwdbppt')
	if(edition == "Normal") {
		$('.editionText').text('专业版')
		$('.Toggle2').attr('edition-key' , 'advanced')
		$('#App').removeClass('ProfessionalEdition')
		$('#generate').text('立即生成')
		$('.Universal').show()
		$('.aigcTips').hide()
		if(is) changeType($('#type_s'))
	}else if(edition == "advanced") {
		$('#App').addClass('ProfessionalEdition')
		$('.editionText').text('极速版')
		$('.Toggle2').attr('edition-key' , 'Normal')
		if(is) changeType($('#type_s2'))
	}else {
		if(edition == 'zjcaigc') {
			$('.Universal').hide()
			$('.aigcTips').show()
			$('#generate').text('开始降AIGC率')
		}else {
			$('.Universal').show()
			$('.aigcTips').hide()
			$('#generate').text('立即生成')
		}
		$('#App').removeClass('ProfessionalEdition')
		$('#App').addClass(edition)
	}
	$(function() {
		setTimeout(function(){
			window.setHeight()
		} , 0)
	});
	window.sessionStorage.setItem('editionKey' , edition)
	if(!$('#App').hasClass('indexApp') && is) {
		location.href = './index.html'
	}
}

// 参数携带
if(getQueryVariable('editionType')) {
	if(getQueryVariable('editionType') == 'Normal') {
		editionType("advanced")
	}else {
		editionType("Normal")
	}
}else {
	if($('#App').hasClass('indexApp') && window.sessionStorage.getItem('editionKey')=='zjcaigc') {
		editionType("Normal")
	} else {
		editionType(window.sessionStorage.getItem('editionKey') || "Normal")
	}
	// editionType("Normal") // 关闭专业版, 打开这一行
}

// 英文参考文献判断
function LiteratureFU() {
	var LiteratureType = $('input:checkbox[name="LiteratureType"]').is(':checked');
	if(LiteratureType && !$('#NumberEnglishReference').val()) {
		$("#LiteratureType_err").show()
		$(".inputQuantity .form-control").addClass('errorBorder')
		return false
	}else {
		$("#LiteratureType_err").hide()
		$(".inputQuantity .form-control").removeClass('errorBorder')
		return true
	}
}
